package com.alibaba.citrus.ots.unified.inventory.facade.service;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.BaselineGetOrganizationById2Request;
import com.epoch.app.bcots.model.dto.BatchQueryWarehouseByIdsRequest;
import com.epoch.app.bcots.model.dto.GetLoginEmployeeInfoResponse;
import com.epoch.app.bcots.model.dto.RoleSDO;
import com.epoch.app.bcots.model.dto.SearchCargoZoneListRequest;
import com.epoch.app.bcots.model.dto.SearchCargoZoneListResponse;
import com.epoch.app.bcots.model.dto.WarehouseInfo;
import com.epoch.app.bcots.service.BaseDataService;
import com.epoch.app.bcots.service.UnifiedInventoryService;
import com.epoch.app.otsplatformenhance.sdo.OrganizationSDO;
import com.epoch.app.otsplatformenhance.service.OtsPlatformEnhanceService;
import com.epoch.app.otsunifiedinventory.api.forecastwarehouse.dto.GetWarehouseByIdRequest;
import com.epoch.app.otsunifiedinventory.api.forecastwarehouse.dto.QueryDeliveryWarehouseByTypeForAllocateOrderRequest;
import com.epoch.app.otsunifiedinventory.api.forecastwarehouse.dto.QueryDeliveryWarehouseByTypeRequest;
import com.epoch.app.otsunifiedinventory.api.forecastwarehouse.dto.QueryReceivingWarehouseByTypeForAllocateOrderRequest;
import com.epoch.app.otsunifiedinventory.api.forecastwarehouse.dto.QueryReceivingWarehouseByTypeRequest;
import com.epoch.app.otsunifiedinventory.api.forecastwarehouse.dto.QueryWarehouseByFactoryCodeAndWarehouseCodeRequest;
import com.epoch.app.otsunifiedinventory.api.forecastwarehouse.service.ForecastWarehouseService;
import com.epoch.app.otsunifiedinventory.dto.response.DeliveryWarehouseChoiceDTO;
import com.epoch.app.otsunifiedinventory.model.dto.WarehouseInfoDTO;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 预报单仓库服务
 */
@Component
@Primary
public class ForecastWarehouseSerivceImpl implements ForecastWarehouseService {

    private final static String DEFAULT = "DEFAULT";
    //分子公司业务员
    private static final String SALES_MAN_ROLE_CODE = "001";
    Log log = Log.getLogger(ForecastWarehouseSerivceImpl.class);

    @Autowired
    private UnifiedInventoryService unifiedInventoryService;
    @Autowired
    private OtsPlatformEnhanceService otsPlatformEnhanceService;
    @Resource
    private BaseDataService baseDataService;
    @Override
    public Result<List<DeliveryWarehouseChoiceDTO>> queryDeliveryWarehouseByType(QueryDeliveryWarehouseByTypeRequest queryDeliveryWarehouseByTypeRequest) {
        /**
         * 	发货仓库：
         * 	单据类型1:默认为第一个可选择的工厂类型的仓库；
         * 	单据类型2:可选仓库类型为工厂、共享仓；默认选择第一个；
         * 	单据类型3、4:仅可选择共享仓，默认选择第一个；
         */
        List<DeliveryWarehouseChoiceDTO> deliveryWarehouseChoiceDTOS = new ArrayList<>();
        SearchCargoZoneListRequest searchCargoZoneListRequest = new SearchCargoZoneListRequest();
        //1启用状态
        searchCargoZoneListRequest.setStatus(1);
        //1是仓库档案，3是逻辑仓
        searchCargoZoneListRequest.setModel(3);
        searchCargoZoneListRequest.setName(queryDeliveryWarehouseByTypeRequest.getName());
        if (StringUtils.isNotEmpty(queryDeliveryWarehouseByTypeRequest.getCode())){
            searchCargoZoneListRequest.setCode(queryDeliveryWarehouseByTypeRequest.getCode());
        }
        String type = queryDeliveryWarehouseByTypeRequest.getType();
        searchCargoZoneListRequest.setSize(99);
        Result<List<OrganizationSDO>> loginEmployeeSaleOrganization = otsPlatformEnhanceService.getLoginEmployeeSaleOrganization();
        List<String> codeList = null;
        if (Objects.nonNull(loginEmployeeSaleOrganization)&&Objects.nonNull(loginEmployeeSaleOrganization.getResult())) {
            Set<String> codes = loginEmployeeSaleOrganization.getResult().stream().map(OrganizationSDO::getId).collect(Collectors.toSet());
            if (CollectionUtils.isNotEmpty(codes)) {
                codeList = new ArrayList(codes);
            }
        }
        GetLoginEmployeeInfoResponse loginEmployeeInfo = baseDataService.getLoginEmployeeInfo();
        if (loginEmployeeInfo != null && loginEmployeeInfo.getOrganizationSDO()!= null){
            boolean isSalesMan = false;
            com.epoch.app.bcots.model.dto.OrganizationSDO organizationSDO = loginEmployeeInfo.getOrganizationSDO();
            if (CollectionUtils.isNotEmpty(loginEmployeeInfo.getRoles())){
                for (RoleSDO role : loginEmployeeInfo.getRoles()) {
                    if (SALES_MAN_ROLE_CODE.equals(role.getCode())){
                        isSalesMan = true;
                    }
                }
                //如果是业务员的 需要找到业务员的销售组织这一层 而非分子公司
                if (isSalesMan){
                    while (!Objects.equals(104,organizationSDO.getType())){
                        BaselineGetOrganizationById2Request baselineGetOrganizationById2Request =
                                new BaselineGetOrganizationById2Request();
                        if (StringUtils.isBlank(organizationSDO.getParentId())){
                            log.info("ForecastWarehouseSerivceImpl.queryDeliveryWarehouseByType.salesMan.org.errResult:{}",JSON.toJSONString(organizationSDO));
                            break;
                        }
                        baselineGetOrganizationById2Request.setId(organizationSDO.getParentId());
                        Result<com.epoch.app.bcots.model.dto.OrganizationSDO> organizationSDOResult = baseDataService.baselineGetOrganizationById2(baselineGetOrganizationById2Request);
                        if (organizationSDOResult == null || organizationSDOResult.getResult() == null){
                            log.info("ForecastWarehouseSerivceImpl.queryDeliveryWarehouseByType.salesMan.org.errResult:{}",JSON.toJSONString(organizationSDOResult));
                            break;
                        }
                        organizationSDO = organizationSDOResult.getResult();
                        if (organizationSDO.getType() != null && organizationSDO.getType() < 104){
                            log.info("ForecastWarehouseSerivceImpl.queryDeliveryWarehouseByType.salesMan.org.errResult:{}",JSON.toJSONString(organizationSDOResult));
                            break;
                        }
                        //如果是分子公司 就把值给填进去 查到销售组织这一层级就可以
                        if (Objects.equals(104,organizationSDO.getType())){
                            if (codeList == null){
                                codeList = Lists.newArrayList();
                            }else {
                                codeList.clear();
                            }
                            codeList.add(organizationSDO.getId());
                        }

                    }
                }
            }
        }
        if (Objects.isNull(type)){
            List<Integer> types = new ArrayList<>();
            types.add(2);
            types.add(5);
            searchCargoZoneListRequest.setTypeList(types);
            deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
        }else{
            //计划调拨 查工厂 不做组织过滤
            if ("1".equals(type)) {
                searchCargoZoneListRequest.setTypeList(Lists.newArrayList(5));
                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
            }

            //销售组织 工厂数据返回
            if ("2".equals(type)) {
                //同是总部销售调拨单, 如果是工厂直发的 F-b那就只查出工厂
                //2共享仓  3销售单元仓 5工厂仓
                if(Objects.equals(Boolean.TRUE,queryDeliveryWarehouseByTypeRequest.getIsDTC())){
                    //如果是工厂直发的 现在放出F1共享仓 即新增F1-b
                    List<Integer> types = new ArrayList<>();
                    types.add(5);
                    searchCargoZoneListRequest.setTypeList(Lists.newArrayList(types));
                    deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));

                    //共享仓 权限过滤
                    types = new ArrayList<>();
                    types.add(2);
                    searchCargoZoneListRequest.setTypeList(types);
                    searchCargoZoneListRequest.setSaleOrgIdList(codeList);
                    deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));

                }else {
                    //如果是非工厂直发的总部销售  F-B F1-B
                    //工厂仓 不做权限过滤
                    List<Integer> types = new ArrayList<>();
                    types.add(5);
                    searchCargoZoneListRequest.setTypeList(Lists.newArrayList(types));
                    deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
                    //共享仓 权限过滤
                    types = new ArrayList<>();
                    types.add(2);
                    searchCargoZoneListRequest.setTypeList(Lists.newArrayList(types));
                    searchCargoZoneListRequest.setSaleOrgIdList(codeList);
                    deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
                }
            }


//            //销售组织 工厂数据返回
//            if ("2".equals(type)) {
//
//                List<Integer> types = new ArrayList<>();
//                types.add(2);
//                types.add(5);
//                searchCargoZoneListRequest.setTypeList(Lists.newArrayList(types));
//                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
////                List<Integer> types = new ArrayList<>();
////                types.add(2);
////                searchCargoZoneListRequest.setTypeList(types);
////                searchCargoZoneListRequest.setSaleOrgIdList(codeList);
////                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
//            }
            //同公司调拨 共享仓 组织过滤
            if ("3".equals(type)) {
                List<Integer> types = new ArrayList<>();
                types.add(2);
                searchCargoZoneListRequest.setTypeList(types);
                searchCargoZoneListRequest.setSaleOrgIdList(codeList);
                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
            }
            //查全部
            if ("4".equals(type)) {
                searchCargoZoneListRequest.setTypeList(Lists.newArrayList(2));
                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
            }
        }



        return Result.success(deliveryWarehouseChoiceDTOS);
    }

    /**
     * 收货仓
     * @param queryReceivingWarehouseByTypeRequest
     * @return
     */
    @Override
    public Result<List<DeliveryWarehouseChoiceDTO>> queryReceivingWarehouseByType(QueryReceivingWarehouseByTypeRequest queryReceivingWarehouseByTypeRequest) {
        /**
         * 	收货仓库：选择提报组织下的逻辑仓库；
         * 	取值范围：通过所选的提报组织，查询对应的逻辑库（货区）
         * 	展示方式：仓库名称 / 货区名称
         * 	选择范围
         * 	单据类型1、3、4，仅可选择共享仓；
         * 	单据类型2、仅可选择销售单元仓；
         */
        List<DeliveryWarehouseChoiceDTO> deliveryWarehouseChoiceDTOS = new ArrayList<>();
        SearchCargoZoneListRequest searchCargoZoneListRequest = new SearchCargoZoneListRequest();
        //1启用状态
        searchCargoZoneListRequest.setStatus(1);
        //1是仓库档案，3是逻辑仓
        searchCargoZoneListRequest.setModel(3);
        searchCargoZoneListRequest.setName(queryReceivingWarehouseByTypeRequest.getName());

        if (StringUtils.isNotEmpty(queryReceivingWarehouseByTypeRequest.getCode())){
            searchCargoZoneListRequest.setCode(queryReceivingWarehouseByTypeRequest.getCode());
        }
        searchCargoZoneListRequest.setSize(99);
        //查找销售组织
        String type = queryReceivingWarehouseByTypeRequest.getType();
        Result<List<OrganizationSDO>> loginEmployeeSaleOrganization = otsPlatformEnhanceService.getLoginEmployeeSaleOrganization();
        if (Objects.nonNull(loginEmployeeSaleOrganization)&&Objects.nonNull(loginEmployeeSaleOrganization.getResult())){
            Set<String> codes = loginEmployeeSaleOrganization.getResult().stream().map(OrganizationSDO::getId).collect(Collectors.toSet());
            if (CollectionUtils.isNotEmpty(codes)){
                searchCargoZoneListRequest.setSaleOrgIdList(new ArrayList(codes));
                if (Objects.isNull(type)){
                    List<Integer> types = new ArrayList<>();
                    types.add(2);
                    types.add(3);
                    searchCargoZoneListRequest.setTypeList(types);
                    deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
                }else{
                    if ("1".equals(type)||"3".equals(type)||"4".equals(type)){
                        //查询共享仓
                        searchCargoZoneListRequest.setTypeList(Lists.newArrayList(2));
                        deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
                    }
                    if ("2".equals(type)){
                        //销售单元仓
                        searchCargoZoneListRequest.setTypeList(Lists.newArrayList(3));
                        deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
                    }
                }
            }
        }
        return Result.success(deliveryWarehouseChoiceDTOS);
    }
    private List<DeliveryWarehouseChoiceDTO> searchCargoZoneList(SearchCargoZoneListRequest searchCargoZoneListRequest) {
        SearchCargoZoneListResponse searchCargoZoneListResponse = unifiedInventoryService.searchCargoZoneList(searchCargoZoneListRequest);
        List<WarehouseInfo> result = searchCargoZoneListResponse.getResult();
        List<DeliveryWarehouseChoiceDTO> deliveryWarehouseChoiceDTOS = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(result)){
            for (WarehouseInfo warehouseInfo : result) {
                DeliveryWarehouseChoiceDTO dto = new DeliveryWarehouseChoiceDTO();
                dto.setValue(warehouseInfo.getId());
                dto.setLabel(warehouseInfo.getName());
                dto.setSapDeliveryBPCode(warehouseInfo.getSapDeliveryBPCode());
                dto.setSapSaleBPCode(warehouseInfo.getSapSaleBPCode());
                dto.setSapInternalSalesBPCode(warehouseInfo.getSapInternalSalesBPCode());
                dto.setSapInternalDeliveryBPCode(warehouseInfo.getSapInternalDeliveryBPCode());
                deliveryWarehouseChoiceDTOS.add(dto);
            }
        }
        return deliveryWarehouseChoiceDTOS;

    }


    @Override
    public Result<DeliveryWarehouseChoiceDTO> getWarehouseById(GetWarehouseByIdRequest getWarehouseByIdRequest){
        if (Objects.nonNull(getWarehouseByIdRequest)&&Objects.nonNull(getWarehouseByIdRequest.getId())){
            BatchQueryWarehouseByIdsRequest batchQueryWarehouseByIdsRequest = new BatchQueryWarehouseByIdsRequest();
            List<String> list = new ArrayList<>();
            list.add(getWarehouseByIdRequest.getId());
            batchQueryWarehouseByIdsRequest.setWarehouseIds(list);
            batchQueryWarehouseByIdsRequest.setModelList(Lists.newArrayList(1,3));
            Object data = unifiedInventoryService.batchQueryWarehouseByIds(batchQueryWarehouseByIdsRequest);
            DeliveryWarehouseChoiceDTO deliveryWarehouseChoiceDTO = new DeliveryWarehouseChoiceDTO();
            if (Objects.nonNull(data)) {
                JSONObject js = JSON.parseObject(JSON.toJSONString(data));
                if (Objects.nonNull(js)) {
                    JSONArray array = js.getJSONArray("result");
                    if (Objects.nonNull(array) && array.size() > 0){
                        JSONObject object = array.getJSONObject(0);
                        if (Objects.nonNull(object)&&Objects.nonNull(object.getJSONObject("features"))){
                            JSONObject features = object.getJSONObject("features");
                            deliveryWarehouseChoiceDTO.setType(object.getString("model"));
                            deliveryWarehouseChoiceDTO.setValue(object.getString("id"));
                            deliveryWarehouseChoiceDTO.setLabel(object.getString("name"));
                            //总部内部售达方编号
                            deliveryWarehouseChoiceDTO.setSapInternalSalesBPCode(features.getString("sapInternalSalesBPCode"));
                            //总部内部送达方编号
                            deliveryWarehouseChoiceDTO.setSapInternalDeliveryBPCode(features.getString("sapInternalDeliveryBPCode"));
                            //总部售达方编号
                            deliveryWarehouseChoiceDTO.setSapSaleBPCode(features.getString("sapSaleBPCode"));
                            //总部送达方编号
                            deliveryWarehouseChoiceDTO.setSapDeliveryBPCode(features.getString("sapDeliveryBPCode"));
                        }
                    }
                }
            }
            return Result.success(deliveryWarehouseChoiceDTO);
        }
        return Result.success(new DeliveryWarehouseChoiceDTO());
    }

    @Override
    public Result<List<DeliveryWarehouseChoiceDTO>> queryDeliveryWarehouseByTypeForAllocateOrder(QueryDeliveryWarehouseByTypeForAllocateOrderRequest queryDeliveryWarehouseByTypeForAllocateOrderRequest) {
        /**
         * 	发货仓库：
         * 	单据类型1:默认为第一个可选择的工厂类型的仓库；
         * 	单据类型2:可选仓库类型为工厂、共享仓；默认选择第一个；
         * 	单据类型3、4:仅可选择共享仓，默认选择第一个；
         */
        List<DeliveryWarehouseChoiceDTO> deliveryWarehouseChoiceDTOS = new ArrayList<>();
        SearchCargoZoneListRequest searchCargoZoneListRequest = new SearchCargoZoneListRequest();
        //1启用状态
        searchCargoZoneListRequest.setStatus(1);
        //1是仓库档案，3是逻辑仓
        searchCargoZoneListRequest.setModel(3);
        searchCargoZoneListRequest.setName(queryDeliveryWarehouseByTypeForAllocateOrderRequest.getName());
        String type = queryDeliveryWarehouseByTypeForAllocateOrderRequest.getType();
        searchCargoZoneListRequest.setSize(99);
        if (StringUtils.isNotEmpty(queryDeliveryWarehouseByTypeForAllocateOrderRequest.getCode())){
            searchCargoZoneListRequest.setCode(queryDeliveryWarehouseByTypeForAllocateOrderRequest.getCode());
        }
        Result<List<OrganizationSDO>> loginEmployeeSaleOrganization = otsPlatformEnhanceService.getLoginEmployeeSaleOrganization();
        List<String> codeList = null;
        if (Objects.nonNull(loginEmployeeSaleOrganization)&&Objects.nonNull(loginEmployeeSaleOrganization.getResult())) {
            Set<String> codes = loginEmployeeSaleOrganization.getResult().stream().map(OrganizationSDO::getId).collect(Collectors.toSet());
            if (CollectionUtils.isNotEmpty(codes)) {
                codeList = new ArrayList(codes);
            }
        }
        if (Objects.isNull(type)){
            List<Integer> types = new ArrayList<>();
            types.add(2);
            types.add(5);
            searchCargoZoneListRequest.setTypeList(types);
            deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
        }else{
            //计划调拨 查工厂 不做组织过滤
            if ("1".equals(type)) {
                searchCargoZoneListRequest.setTypeList(Lists.newArrayList(5));
                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
            }
            //销售组织 工厂数据返回
            if ("2".equals(type)) {
//                searchCargoZoneListRequest.setTypeList(Lists.newArrayList(5));
//                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
                List<Integer> types = new ArrayList<>();
                types.add(2);
                searchCargoZoneListRequest.setTypeList(types);
                searchCargoZoneListRequest.setSaleOrgIdList(codeList);
                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
            }
            //同公司调拨 共享仓 组织过滤
            if ("3".equals(type)) {
                List<Integer> types = new ArrayList<>();
                types.add(2);
                searchCargoZoneListRequest.setTypeList(types);
                searchCargoZoneListRequest.setSaleOrgIdList(codeList);
                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
            }
            //查全部
            if ("4".equals(type)) {
                searchCargoZoneListRequest.setTypeList(Lists.newArrayList(2));
                deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
            }
        }



        return Result.success(deliveryWarehouseChoiceDTOS);
    }

    @Override
    public Result<List<DeliveryWarehouseChoiceDTO>> queryReceivingWarehouseByTypeForAllocateOrder(QueryReceivingWarehouseByTypeForAllocateOrderRequest queryReceivingWarehouseByTypeForAllocateOrderRequest) {
        /**
         * 	收货仓库：选择提报组织下的逻辑仓库；
         * 	取值范围：通过所选的提报组织，查询对应的逻辑库（货区）
         * 	展示方式：仓库名称 / 货区名称
         * 	选择范围
         * 	单据类型1、3、4，仅可选择共享仓；
         * 	单据类型2、仅可选择销售单元仓；
         */
        List<DeliveryWarehouseChoiceDTO> deliveryWarehouseChoiceDTOS = new ArrayList<>();
        SearchCargoZoneListRequest searchCargoZoneListRequest = new SearchCargoZoneListRequest();
        //1启用状态
        searchCargoZoneListRequest.setStatus(1);
        //1是仓库档案，3是逻辑仓
        searchCargoZoneListRequest.setModel(3);
        searchCargoZoneListRequest.setName(queryReceivingWarehouseByTypeForAllocateOrderRequest.getName());
        searchCargoZoneListRequest.setSize(99);
        if (StringUtils.isNotEmpty(queryReceivingWarehouseByTypeForAllocateOrderRequest.getCode())){
            searchCargoZoneListRequest.setCode(queryReceivingWarehouseByTypeForAllocateOrderRequest.getCode());
        }
        //查找销售组织
        String type = queryReceivingWarehouseByTypeForAllocateOrderRequest.getType();
        Result<List<OrganizationSDO>> loginEmployeeSaleOrganization = otsPlatformEnhanceService.getLoginEmployeeSaleOrganization();
        if (Objects.nonNull(loginEmployeeSaleOrganization)&&Objects.nonNull(loginEmployeeSaleOrganization.getResult())){
            Set<String> codes = loginEmployeeSaleOrganization.getResult().stream().map(OrganizationSDO::getId).collect(Collectors.toSet());
            if (CollectionUtils.isNotEmpty(codes)){
                searchCargoZoneListRequest.setSaleOrgIdList(new ArrayList(codes));
                if (Objects.isNull(type)){
                    List<Integer> types = new ArrayList<>();
                    types.add(2);
                    types.add(3);
                    searchCargoZoneListRequest.setTypeList(types);
                    deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
                }else{
                    if ("1".equals(type)||"3".equals(type)||"4".equals(type)){
                        //查询共享仓
                        searchCargoZoneListRequest.setTypeList(Lists.newArrayList(2));
                        deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
                    }
                    if ("2".equals(type)){
                        //销售单元仓
                        searchCargoZoneListRequest.setTypeList(Lists.newArrayList(3));
                        deliveryWarehouseChoiceDTOS.addAll(searchCargoZoneList(searchCargoZoneListRequest));
                    }
                }
            }
        }
        return Result.success(deliveryWarehouseChoiceDTOS);
    }

    @Override
    public Result<List<WarehouseInfoDTO>> queryWarehouseByFactoryCodeAndWarehouseCode(QueryWarehouseByFactoryCodeAndWarehouseCodeRequest request) {
        //收货id 根据工厂和库存code查找,查不到将库存code设置成DEFAULT 在查一遍
        List<WarehouseInfoDTO> warehouseInfoDTOS = new ArrayList<>();
        SearchCargoZoneListRequest searchCargoZoneListRequest = new SearchCargoZoneListRequest();
        if (Objects.isNull(request.getWarehouseCode())||StringUtils.isEmpty(request.getWarehouseCode())) {
            searchCargoZoneListRequest.setWarehouseCode(DEFAULT);
        } else {
            searchCargoZoneListRequest.setWarehouseCode(request.getWarehouseCode());
        }
        searchCargoZoneListRequest.setSapFactoryCode(request.getFactoryCode());
        SearchCargoZoneListResponse receiveResponse = unifiedInventoryService.searchCargoZoneList(searchCargoZoneListRequest);
        List<WarehouseInfo> warehouseInfos = new ArrayList<>();
        if(Objects.nonNull(receiveResponse)){
            warehouseInfos = receiveResponse.getResult();
        }
        if (CollectionUtils.isEmpty(warehouseInfos) &&!DEFAULT.equals(searchCargoZoneListRequest.getWarehouseCode())){
            searchCargoZoneListRequest.setWarehouseCode(DEFAULT);
            receiveResponse = unifiedInventoryService.searchCargoZoneList(searchCargoZoneListRequest);
        }
        if (Objects.nonNull(receiveResponse)&&CollectionUtils.isNotEmpty(receiveResponse.getResult())){
            for (WarehouseInfo warehouseInfo :receiveResponse.getResult()){
                WarehouseInfoDTO warehouseInfoDTO = new WarehouseInfoDTO();
                BeanUtils.copyProperties(warehouseInfo,warehouseInfoDTO);
                warehouseInfoDTOS.add(warehouseInfoDTO);
            }
        }
        return Result.success(warehouseInfoDTOS);
    }
}
